package com.ggh.smartreply;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;


public class SmartReplyService extends Service {

    private SMSBroadcastReceiver smsBroadcastReceiver = null;
    private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        smsBroadcastReceiver = new SMSBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SMS_ACTION);
        SmartReplyService.this.registerReceiver(smsBroadcastReceiver,filter);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        SmartReplyService.this.unregisterReceiver(smsBroadcastReceiver);
        super.onDestroy();
    }



    public class SMSBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
                Object[] pdus=(Object[])intent.getExtras().get("pdus");
                SmsMessage[] message=new SmsMessage[pdus.length];

                RuleDataBaseHelper ruleDataBaseHelper = new RuleDataBaseHelper(SmartReplyService.this,"text_RULE");
                SQLiteDatabase db = ruleDataBaseHelper.getReadableDatabase();
                Cursor cursor = db.query("RULE",null,null,null,null,null,null,null);

                for(int i=0;i<pdus.length;i++){
                    message[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
                    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        if (message[i].getDisplayMessageBody().contains(cursor.getString(cursor.getColumnIndex("_check")))){

                            new SmartReplyer().replyer(message[i].getOriginatingAddress() , cursor.getString(cursor.getColumnIndex("_reply")));
                            db.close();

                        }
                    }
                }

                db.close();
            }
        }
    }

    public class SmartReplyer{

        public void replyer(String destinationAddress , String text){

            SmsManager smsManager= SmsManager.getDefault();
            smsManager.sendTextMessage(destinationAddress, null, text, null, null);
            ContentValues values = new ContentValues();
            values.put("date", System.currentTimeMillis());
            values.put("read", 1);
            values.put("type", 2);
            values.put("address",destinationAddress);
            values.put("body", text);
            getContentResolver().insert(Uri.parse("content://sms/sent"), values);

        }

    }

}